import React, { Component } from 'react'
import $http from '../http';

export default class Login extends Component {
    state={
        username:'',
        password:''
    }
    login(){
        $http.post('/api/login',{username:this.state.username,password:this.state.password}).then((res)=>{
          alert(res.message);
           if(res.code===0){
                sessionStorage.setItem('token',res.token);
                this.props.history.push('/my');
           }else{
               sessionStorage.removeItem('token');
           }
        })
    }
    render() {
        let {username,password}=this.state;
        return (
            <div>
                <h2>登录</h2>
                <p>用户名： <input type="text"  value={username} onChange={(e)=>{
                   this.setState({username:e.target.value});
                }}  /></p>
                <p>密码： <input type="text"  value={password} onChange={(e)=>{
                    this.setState({password:e.target.value})
                    
                }}  /></p>
                <button onClick={()=>{
                    this.login();
                }}>登录</button>
            </div>
        )
    }
}
